//
//  FindViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "FindViewController.h"
#import "WHNLiveCollectionViewCell.h"
#import "WHNTopListCollectionViewCell.h"
#import "WWCategoryCollectionViewCell.h"
#import "AlbumFViewController.h"
#import "WHNFirstViewController.h"
#import "MoreTableViewController.h"
#import "WWCategoryViewController.h"
#import "WHNSecondViewController.h"
#import "WHNThirdViewController.h"
#import "WHNFourthViewController.h"
#import "FEditTableViewController.h"
#import "WWListenListViewController.h"

@interface FindViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, MenuControlViewDelegate, WHNPassStringDelegate,WWCategoryCollectionViewCellDelegate, WHNPassKeyAndTypeDelegate>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)MenuControlView *menuView;
@property(nonatomic, strong)NSMutableArray *firstArray;
@property(nonatomic, strong)NSMutableArray *secondArray;
@property(nonatomic, strong)NSMutableArray *thirdArray;
@property(nonatomic, strong)NSMutableArray *fourthArray;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

static NSString *systemCell = @"systemCell";

@implementation FindViewController

#pragma mark - 懒加载
#pragma mark 初始化collectionView
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake(GViewWidth(self.view), GViewHeight(self.view) - 64 - 49 - FitConstant_YorHeight(50));
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, FitConstant_YorHeight(50), self.view.frame.size.width, GViewHeight(self.view) - 64 - 49 - FitConstant_YorHeight(50)) collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView NightWithType:UIViewColorTypeNormal];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:systemCell];
        //注册cell  ww
         [self.collectionView registerClass:[WWCategoryCollectionViewCell class] forCellWithReuseIdentifier:@"wwCell"];
        //注册cell  whn
        [_collectionView registerClass:[WHNLiveCollectionViewCell class] forCellWithReuseIdentifier:@"WHNcell"];
        [_collectionView registerClass:[FYCollectionViewCell class] forCellWithReuseIdentifier:@"fyCell"];
        [_collectionView registerClass:[WHNLiveCollectionViewCell class] forCellWithReuseIdentifier:@"WHNtwo"];
        [_collectionView registerClass:[WHNTopListCollectionViewCell class] forCellWithReuseIdentifier:@"WHNthree"];
    }
    return _collectionView;
}

#pragma mark 初始化menuView
-(MenuControlView *)menuView
{
    if (!_menuView) {
        _menuView = [[MenuControlView alloc] initWithFrame:FitRectMake(0, 0, 375, 50)];
        _menuView.delegate = self;
        _menuView.numberOfMenu = 4;
        _menuView.dataSource = @[@"推荐", @"分类", @"直播", @"榜单"];
    }
    return _menuView;
}

-(void)viewWillAppear:(BOOL)animated
{
    self.hidesBottomBarWhenPushed = NO;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"发现";
    
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.collectionView];
    
    UIImage *image = [UIImage imageNamed:@"icon-sousuo"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonAction)];
}

-(void)rightBarButtonAction
{
    SearchViewController *vc = [[SearchViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
//    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:NO];
}


#pragma mark - collectionView代理方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fyCell" forIndexPath:indexPath];

        cell.block = ^(NSString *trackId, NSString *title){
            if ([trackId isEqualToString:@"-1"]) {
                MoreTableViewController *vc = [[MoreTableViewController alloc] init];
                vc.trankId = trackId;
                self.tabBarController.tabBar.hidden = YES;
                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
            else if ([title isEqualToString:@"小编"]){
                FEditTableViewController *vc = [[FEditTableViewController alloc] init];
//                self.tabBarController.tabBar.hidden = YES;
                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
            else{
                WWCategoryViewController *category = [[WWCategoryViewController alloc] init];
                category.categoryId = [trackId integerValue];
                category.categoryTitle = title;
                self.tabBarController.tabBar.hidden = YES;
//                self.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:category animated:NO];
            }
        };
        cell.delegate = self;
        return cell;
    }
    if (indexPath.row == 1) {
        WWCategoryCollectionViewCell *wwCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wwCell" forIndexPath:indexPath];
        wwCell.wwDelegate = self;
        return wwCell;
    }
    
    if (indexPath.row == 2) {
        WHNLiveCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WHNtwo" forIndexPath:indexPath];
        cell.block = ^(NSString *pic, NSString *count, NSString *radioId, NSString *rname){
            WHNThirdViewController *thirdVC = [[WHNThirdViewController alloc]init];
            thirdVC.WHNCount = count;
            thirdVC.WHNPic = pic;
            thirdVC.RadioId = radioId;
            thirdVC.Rname = rname;
            self.tabBarController.tabBar.hidden = YES;
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:thirdVC animated:YES];
        };
        cell.moreBlock = ^(NSString *urlString){
            WHNFourthViewController *fourVC = [[WHNFourthViewController alloc]init];
            fourVC.string = urlString;
            self.tabBarController.tabBar.hidden = YES;
//            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:fourVC animated:YES];
        };
        cell.delegate = self;
        return cell;
    }
    if (indexPath.row == 3) {
        WHNTopListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WHNthree" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:systemCell forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor grayColor];
    
    return cell;
}

#pragma mark - scrollView代理方法, 用来控制menuView的切换
#pragma mark 结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = (NSInteger)(scrollView.contentOffset.x + FitConstant_XorWidth(50)) / scrollView.frame.size.width;
    [_menuView selectCell:page];
}

#pragma mark - MenuViewDelegate
-(void)didSelectedMenu:(NSInteger)number
{
    self.collectionView.contentOffset = CGPointMake(self.collectionView.frame.size.width * number, 0);
}

-(void)sendTitle:(NSString *)title TrackID:(NSString *)trackID AlbumID:(NSString *)albumId
{
    AlbumFViewController *vc = [[AlbumFViewController alloc] init];
    vc.alumbTitel = title;
    vc.albumId = albumId;
    vc.trackID = trackID;
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)sendID:(NSInteger )specialId {
    WWListenListViewController *listen = [[WWListenListViewController alloc] init];
    listen.specialId = specialId;
//    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:listen animated:NO];
}

#pragma mark - 直播点击跳转,协议方法
-(void)passString:(NSString *)numString Title:(NSString *)titleString
{
    WHNFirstViewController *firstVC = [[WHNFirstViewController alloc]init];
    firstVC.numString = numString;
    firstVC.titleString = titleString;
    self.tabBarController.tabBar.hidden = YES;//隐藏tabbar
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:firstVC animated:YES];
}

#pragma mark - 榜单,协议方法
-(void)passKey:(NSString *)keyString Type:(NSString *)typeString
{
    WHNSecondViewController *secondVC = [[WHNSecondViewController alloc]init];
    secondVC.typeString = typeString;
    secondVC.keyString = keyString;
    self.tabBarController.tabBar.hidden = YES;
//    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:secondVC animated:YES];
}


#pragma mark - ww代理方法

- (void)wwPushVCWithId:(NSInteger)caretoryId withTitle:(NSString *)title{
    WWCategoryViewController *category = [[WWCategoryViewController alloc] init];
    category.categoryId = caretoryId;
    category.categoryTitle = title;
    self.tabBarController.tabBar.hidden = YES;
//    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:category animated:NO];
}

@end
